<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Channel;
use App\Admin\Repositories\ChannelPeriod;
use App\Models\ChannelPeriod as ChannelPeriodModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Platform as PlatformModel;
use App\Models\Channel as ChannelModel;

class ChannelController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Channel(), function (Grid $grid) {
            $grid->column('id');
            $grid->column('name');
            $grid->column('platform_id')->using(PlatformModel::genOptions());
            $grid->column('status')
                ->using(ChannelModel::getStatusOption())
                ->dot(ChannelModel::getStatusColorMap());
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail(mixed $id): Show
    {
        return Show::make($id, new Channel(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('frequency');
            $show->field('platform_id', '所属台站')->using(PlatformModel::genOptions());
            $show->field('status')->using(ChannelModel::getStatusOption());
//            $show->field('created_at');
//            $show->field('updated_at');


            $key = $show->getKey();

            /**
             * 频率时段
             */
            $show->channelSections('时段管理', function ($model) use ($key) {
                $grid = new Grid(new ChannelPeriod());

                $grid->disableRowSelector();

                $grid->enableDialogCreate();

                // 操作按钮
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableView();
                });

                $grid->model()->where('channel_id', $model->id)->orderBy('day_of_week', 'asc');
                $grid->model()->setConstraints(['channel_id' => $key]);

                // 设置路由
                $grid->setResource('channel-periods');

                $grid->column('day_of_week', '周几')->using(ChannelPeriodModel::getDayOfWeekList());
                $grid->column('started_at', '开始时间');
                $grid->column('ended_at', '结束时间');

                $grid->status()->switch();

                return $grid;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Channel(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('frequency')->required();
            $form->select('platform_id', '台站')->options(PlatformModel::genOptions())->required();
            $form->radio('status')
                ->options(ChannelModel::getStatusOption())
                ->default(ChannelModel::STATUS_ENABLE)
                ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
